<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\MiddleTable;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\Response
     */

    public function index()
    {
        $oVideos = Video::paginate(10);
        $oExperts = Expert::paginate(10);
        return view('admin.admin-videolist')->with('oVideos',$oVideos)->with('oExperts',$oExperts);
    }
    public function review(){
        $infos = Video::paginate(9);
        return view('front.review')->with('infos',$infos);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
    public function add()
    {
        $oExpert = Expert::all();
        return view('admin.admin-addvideo')->with('oExpert',$oExpert);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->input();
        $num1 = $request->input('video_type1');
        $num2 = $request->input('video_type2');
        if ($num1 > $num2) {
            $num = 0;
        } else{
            $num = 1;
        }
        $data['type'] = $num;
        $rules = [
            'title' => 'required|string|max:255',
            'url' => 'required',
            'description' => 'nullable|string',
            'begin' => 'required|date',
            'end' => 'required|date|after_or_equal:begin',
            'video_type1' => 'nullable|boolean',
            'video_type2' => 'nullable|boolean',
        ];
        $messages = [
            'title.required' => '标题不能为空。',
            'title.max' => '标题长度不能超过 255 个字符。',
            'url.required' => '视频链接不能为空。',
            'description.string' => '简介只能是纯文本。',
            'begin.required' => '开始日期不能为空。',
            'begin.date' => '开始日期格式不正确。',
            'end.required' => '结束日期不能为空',
            'end.date' => '结束日期格式不正确',
            'end.after_or_equal' => '结束日期不能早于开始日期',
            'video_type1.boolean' => '视频类型参数不正确。',
            'video_type2.boolean' => '视频类型参数不正确。',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);;
        if ($validator->fails())
        {
            return json_encode(['success' => false, 'msg' => $validator->errors()->first()]);
        }else{
            Video::create($data);
//            $expert = new Expert();
//            $expert->title = $request->input('title');
//            $expert->url = $request->input('url');
//            $expert->description = $request->input('description');
//            $expert->begin = $request->input('begin');
//            $expert->end = $request->input('end');
//            $expert->video_type1 = $request->input('video_type1', false);
//            $expert->video_type2 = $request->input('video_type2', false);
//            $expert->save();
//            MiddleTable::create($data[]);
            return json_encode(['success'=>true,'msg'=>'成功提交']);
        }
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        // 获取当前视频信息
        $currentVideo = Video::findOrFail($id);

        // 随机获取2条不包括当前视频的记录
        $relatedVideos = Video::inRandomOrder()
            ->where('id', '<>', $id)
            ->take(2)
            ->get();

        return view('front.video-show', [
            'infos' => $currentVideo,
            'video' => $relatedVideos,
        ]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $oVideo = Video::find($id);
        $infos = $oVideo->experts;
        $oExperts = Expert::paginate(10);
        return view('admin.admin-edit-video')->with('oVideo',$oVideo)->with('oExperts',$oExperts)->with('infos',$infos);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        $data = $request->input();
        $num1 = $request->input('video_type1');
        $num2 = $request->input('video_type2');
        if ($num1 > $num2) {
            $num = 0;
        } else{
            $num = 1;
        }
        $rules = [
            'title' => 'required|string|max:255',
            'url' => 'required',
            'description' => 'nullable|string',
            'begin' => 'required|date',
            'end' => 'required|date|after_or_equal:begin',
            'video_type1' => 'nullable|boolean',
            'video_type2' => 'nullable|boolean',
        ];
        $messages = [
            'title.required' => '标题不能为空。',
            'title.max' => '标题长度不能超过 255 个字符。',
            'url.required' => '视频链接不能为空。',
            'description.string' => '简介只能是纯文本。',
            'begin.required' => '开始日期不能为空。',
            'begin.date' => '开始日期格式不正确。',
            'end.required' => '结束日期不能为空',
            'end.date' => '结束日期格式不正确',
            'end.after_or_equal' => '结束日期不能早于开始日期',
            'video_type1.boolean' => '视频类型参数不正确。',
            'video_type2.boolean' => '视频类型参数不正确。',
        ];
        $validator = Validator::make($request->all(), $rules, $messages);;
        if ($validator->fails())
        {
            return json_encode(['success' => false, 'msg' => $validator->errors()->first()]);
        }else{
            $video = Video::find($id);
            $video->update($data);
            return json_encode(['success'=>true,'msg'=>'成功提交']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)//删除录像功能,已实现
    {
        $data = $request->input();
        $video = Video::find($data['id']);
        $video->delete();
        return redirect('/adm/expert');
    }
}
